<script setup>
import {computed, ref} from 'vue'
import { Plus,Download, Upload } from "@element-plus/icons-vue";
import { http } from "@/api";
import useTable from "@/utils/useTable";
import {useStore} from "vuex";
import {ElMessage} from "element-plus";
import {enterpriseResTemplateDownloadUrl} from "@/utils/common";
/**
 * 搜索
 */

const provinces = ref([])
const store = useStore()
const provincesoptions = computed(() => {
  return store.getters.provincesoptions
})
const handleChangeprovinces = (val) => {
  console.log(val)
  if (!val) {
    searchParams.value.province = ''
    searchParams.value.municipality = ''
    return
  }
  searchParams.value.province = val[0]
  searchParams.value.municipality = val[1]
}
const booleanList = [
  { name: '是', id: 1 },
  { name: '否', id: 0 },
]
const categoryList = ref([])
http.industryCategory.list().then(res => {
  categoryList.value = res.data
})
/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openAdd,
  openView,
  openEdit,
  openDelete
} = useTable({
  requestFn: http.enterpriseRes,
  routerPre: 'enterpriseRes',
  params: {
    name: '',
    categoryId: ''
  }
})
const downloadTemplate = () => {
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = enterpriseResTemplateDownloadUrl
  link.setAttribute('download', '企业资源导入模板.xlsx')
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}
const openUpload = (file) => {
  console.log(file)
  http.enterpriseRes.import(file.file).then(res => {
    ElMessage.success('导入成功')
    search()
  })
}
const changeStatus = (row) => {
  http.enterpriseRes.updateStatus({
    id: row.id,
    isAsterisk: row.isAsterisk
  }).then(res => {
    ElMessage.success('操作成功')
  }).catch(() => {
    if (row.isAsterisk === 1) {
      row.isAsterisk = 0
    } else {
      row.isAsterisk = 1
    }
  })
}
</script>


<template>
  <PageMain title="企业资源">
    <el-form label-suffix="：" inline>
      <el-form-item label="企业名称">
        <el-input clearable v-model="searchParams.name" ></el-input>
      </el-form-item>
      <el-form-item label="企业所在省市">
        <el-cascader clearable v-model="provinces" :options="provincesoptions" @change="handleChangeprovinces" />
      </el-form-item>
      <el-form-item label="企业所属产业">
        <el-select v-model="searchParams.categoryId" clearable>
          <el-option v-for="item in categoryList" :label="item.name" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否重点企业">
        <el-select v-model="searchParams.isAsterisk"  >
          <el-option v-for="item in booleanList" :label="item.name" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="add-box f-r a-c f-e">
      <el-button type="primary" :icon="Download" @click="downloadTemplate">下载模板</el-button>
      <el-upload class="upload-button" :show-file-list="false" :http-request="openUpload" action="#">
        <el-button type="primary" :icon="Upload">导入</el-button>
      </el-upload>
      <el-button type="primary" :icon="Plus" @click="openAdd">添加</el-button>
    </div>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="企业名称" prop="name"></el-table-column>
        <el-table-column label="企业所在省市" prop="province">
          <template #default="scope">
            {{ scope.row.province }}-{{ scope.row.municipality }}
          </template>
        </el-table-column>
        <el-table-column label="所属产业名称" prop="categoryName">
        </el-table-column>
        <el-table-column label="企业图片">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <ViewImage type="image" :image-url="scope.row.imageUrl"/>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="是否重点企业">
          <template #default="scope">
            <el-switch @change="() => changeStatus(scope.row)" :active-value="1" :inactive-value="0" v-model="scope.row.isAsterisk"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="企业成立时间" prop="establishmentTime"></el-table-column>
        <el-table-column label="固定电话" prop="mobile"></el-table-column>
        <el-table-column label="法人" prop="legalPerson"></el-table-column>
        <el-table-column label="操作" fixed="right" width="180px">
          <template #default="scope">
            <el-button type="text" @click="openView(scope.row)">查看</el-button>
            <el-button type="text" @click="openEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="openDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />

    </div>
  </PageMain>
</template>

<style scoped lang="scss">

</style>
